<template>
  <div class="my-container">
    <div class="header no-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="moblie-img" src="~@/assets/mobile.png" alt="" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image class="avator" round fit="cover" :src="userInfo.photo" />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-status">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 导航区 -->
    <van-grid :column-num="2" class="van-grid" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 导航区 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      title="退出登录"
      clickable
      @click="onLoginOut"
      class="login-out-cell"
      v-if="user"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUser } from '@/api/user.js'
export default {
  name: 'myIndex',

  data() {
    return {
      // 当前用户的信息对象
      userInfo: {

      }
    };
  },

  mounted() {
    // 如果用户是登录状态
    if (this.user) {
      this.getUserInfo()
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    onLoginOut() {
      this.$dialog.confirm({
        title: '退出登录',
        message: '确定提出登录吗'
      })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async getUserInfo() {
      try {
        const { data } = await getUser()
        this.userInfo = data.data
      } catch (err) {
        this.$toast('获取用户信息失败，请稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  .no-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .moblie-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      box-sizing: border-box;
      height: 231px;
      // background-color: pink;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
      .avator {
        width: 132px;
        height: 132px;
        margin-right: 23px;
        border: 1px solid #fff;
      }
    }
    .data-status {
      height: 130px;
      // background-color: #ccc;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .van-grid {
    .grid-item {
      height: 141px;
    }
    .toutiao {
      font-size: 45px;
    }
    .text {
      font-size: 28px;
    }
  }
  .login-out-cell {
    text-align: center;
    color: red;
  }
}
</style>